$(function(){
	$('#reservation1').daterangepicker({
		locale : {
			format: 'YYYY-MM-DD',
			separator:' 到 ',
			applyLabel : '确定',
			cancelLabel : '取消',
			customRangeLabel : '自定义',
			daysOfWeek : [ '日', '一', '二', '三', '四', '五', '六' ],
			monthNames : [ '一月', '二月', '三月', '四月', '五月', '六月',
				'七月', '八月', '九月', '十月', '十一月', '十二月' ],
			firstDay : 1
		},
		ranges: {
			'今天': [moment().startOf('day'), moment()],
			'昨天': [moment().subtract(1,'days').startOf('day'), moment().subtract(1,'days').endOf('day')],
			'本周': [moment().startOf("week").add(1,'days'),moment().endOf("week").add(1,'days')],
			'上周': [moment().subtract(1,'weeks').startOf("week").add(1,'days'),moment().subtract(1,'weeks').endOf("week").endOf("week").add(1,'days')],
			'本月': [moment().startOf("month"),moment().endOf("month")],
			'上个月': [moment().subtract(1,"month").startOf("month"),moment().subtract(1,"month").endOf("month")],
			'最近7天': [moment().subtract(6,'days'), moment()],
			'最近30天': [moment().subtract(29,'days'), moment()]
		},
		applyClass:'btn-primary',
		alwaysShowCalendars: true,
		autoclose:true
	});
	$('#reservation2').daterangepicker({
		locale : {
			format: 'YYYY-MM-DD',
			separator:' 到 ',
			applyLabel : '确定',
			cancelLabel : '取消',
			customRangeLabel : '自定义',
			daysOfWeek : [ '日', '一', '二', '三', '四', '五', '六' ],
			monthNames : [ '一月', '二月', '三月', '四月', '五月', '六月',
				'七月', '八月', '九月', '十月', '十一月', '十二月' ],
			firstDay : 1
		},
		ranges: {
			'今天': [moment().startOf('day'), moment()],
			'昨天': [moment().subtract(1,'days').startOf('day'), moment().subtract(1,'days').endOf('day')],
			'本周': [moment().startOf("week").add(1,'days'),moment().endOf("week").add(1,'days')],
			'上周': [moment().subtract(1,'weeks').startOf("week").add(1,'days'),moment().subtract(1,'weeks').endOf("week").endOf("week").add(1,'days')],
			'本月': [moment().startOf("month"),moment().endOf("month")],
			'上个月': [moment().subtract(1,"month").startOf("month"),moment().subtract(1,"month").endOf("month")],
			'最近7天': [moment().subtract(6,'days'), moment()],
			'最近30天': [moment().subtract(29,'days'), moment()]
		},
		applyClass:'btn-primary',
		alwaysShowCalendars: true,
		autoclose:true
	});
	
	//多选框
	$(document).on('change', 'input:checkbox.master1', function(){
		if($(this).prop('checked')){
			$('input:checkbox.slaver1').prop('checked', 'checked');
		}else{
			$('input:checkbox.slaver1').prop('checked', '');
		}
	})
	$(document).on('change', 'input:checkbox.master2', function(){
		if($(this).prop('checked')){
			$('input:checkbox.slaver2').prop('checked', 'checked');
		}else{
			$('input:checkbox.slaver2').prop('checked', '');
		}
	})
})

//reward table初始化
var InitiateRewardsDataTable = function () {
    return {
        init: function () {
            //Datatable Initiating
            var oTable = $('#rewardTable').dataTable({
            	"bPaginate": true,  //是否显示分页
            	"iDisplayLength": 10,
            	"bLengthChange": false,//每页显示的记录数
            	"bFilter": false, //搜索栏
            	"bSort": true, //是否支持排序功能
            	"bInfo": true, //显示表格信息
            	"bAutoWidth": false,  //自适应宽度
            	"bStateSave": true, //保存状态到cookie *************** 很重要 ， 当搜索的时候页面一刷新会导致搜索的消失。使用这个属性就可避免了
            	//"sPaginationType": "", //分页，一共两种样式，full_numbers和two_button(默认)
            	"sAjaxSource" : ctx+'/reward/getAll?type=1',
        		"fnServerData": retrieveData1,//用于替换默认发到服务端的请求操作  
            	"bServerSide": true,
            	"bDestroy": true,
                "bRetrieve": false,
            	"oLanguage" : {
        			"sLengthMenu" : "每页显示 _MENU_ 条记录",
        			"sZeroRecords" : "抱歉， 没有找到",
        			"sInfo" : "从 _START_ 到 _END_ / 共 _TOTAL_ 条数据",
        			"sInfoEmpty" : "找不到相关数据",
        			"sInfoFiltered" : "数据表中共为 _MAX_ 条记录)",
        			"sProcessing": "正在加载中...",
        			"sSearch": "搜索",
        			"oPaginate" : {
        				"sFirst" : "首页",
        				"sPrevious" : "前一页",
        				"sNext" : "后一页",
        				"sLast" : "尾页"
        			},
        		},
        		"aoColumns" : [
        		               	{ "mDataProp": "rewardId","bSortable": false,'sClass': "text-center", "mRender":function(data, type, full){
        		               		return "<label> <input class='slaver1' type='checkbox'> <span class='text'></span> </label>";
        		               	}},
        		               	{ "mDataProp": "sysDepartment.fullName","bSortable": true,'sClass': "text-center"},
        		               	{ "mDataProp": "sysDepartment.fullName","bSortable": true,'sClass': "text-center"},
        		               	{ "mDataProp": "rewardType","bSortable": true,'sClass': "text-center"}, 
        		               	{ "mDataProp": "eventDate","bSortable": true,'sClass': "text-center"},
        		               	{ "mDataProp": "submitDate","bSortable": true,'sClass': "text-center"},
        		               	{ "mDataProp": "dealDate","bSortable": true,'sClass': "text-center"},
        		               	{ "mDataProp": "sysUser.realName","bSortable": true,'sClass': "text-center"},
        		               	{ "mDataProp": "rewardMoney","bSortable": true,'sClass': "text-center"},
        		               	{ "mDataProp": "effectDate","bSortable": false,'sClass': "text-center"}
        		   			],
	   			"aoColumnDefs": [{
	   	            sDefaultContent: '',
	   	            aTargets: ['_all']
	   	        }],
            	
            });

        }

    };

}();

InitiateRewardsDataTable.init();

function retrieveData1( sSource, aoData, fnCallback, oSettings ) {
	
    aoData.push( { "name": "pageNum", "value": (Math.ceil( oSettings._iDisplayStart / oSettings._iDisplayLength )+1) });
    aoData.push( { "name": "pageSize", "value": oSettings._iDisplayLength });  
    
    var dateRange = $('#reservation1').val();
    var searchVal = $('#searchVal1').val();
    
    if(dateRange && dateRange.length != 0){
    	var minDate = trim(dateRange.split("-")[0]);
        var maxDate = trim(dateRange.split("-")[1]);
        
        aoData.push({ "name": "minDate", "value": minDate });
        aoData.push({ "name": "maxDate", "value": maxDate });
    }
    
    if(searchVal && searchVal.length != 0){
    	aoData.push({ "name": "searchVal", "value": searchVal });
    }
    
    $.ajax( {  
        "url": sSource,  
        "data": aoData,  
        "cache": false,  
        "dataType": 'json', 
        "type": "POST", 
        "success" :function(response) {  
        	fnCallback(response.returnObject);
        }  
    } );  
}  



//pubish table初始化
var InitiatePunishDataTable = function () {
    return {
        init: function () {
            //Datatable Initiating
            var oTable = $('#punishTable').dataTable({
            	"bPaginate": true,  //是否显示分页
            	"iDisplayLength": 10,
            	"bLengthChange": false,//每页显示的记录数
            	"bFilter": false, //搜索栏
            	"bSort": true, //是否支持排序功能
            	"bInfo": true, //显示表格信息
            	"bAutoWidth": false,  //自适应宽度
            	"bStateSave": true, //保存状态到cookie *************** 很重要 ， 当搜索的时候页面一刷新会导致搜索的消失。使用这个属性就可避免了
            	//"sPaginationType": "", //分页，一共两种样式，full_numbers和two_button(默认)
            	"sAjaxSource" : ctx+'/reward/getAll?type=2',
        		"fnServerData": retrieveData2,//用于替换默认发到服务端的请求操作  
            	"bServerSide": true,
            	"bDestroy": true,
                "bRetrieve": false,
            	"oLanguage" : {
        			"sLengthMenu" : "每页显示 _MENU_ 条记录",
        			"sZeroRecords" : "抱歉， 没有找到",
        			"sInfo" : "从 _START_ 到 _END_ / 共 _TOTAL_ 条数据",
        			"sInfoEmpty" : "找不到相关数据",
        			"sInfoFiltered" : "数据表中共为 _MAX_ 条记录)",
        			"sProcessing": "正在加载中...",
        			"sSearch": "搜索",
        			"oPaginate" : {
        				"sFirst" : "首页",
        				"sPrevious" : "前一页",
        				"sNext" : "后一页",
        				"sLast" : "尾页"
        			},
        		},
        		"aoColumns" : [
       		               	{ "mDataProp": "rewardId","bSortable": false,'sClass': "text-center", "mRender":function(data, type, full){
       		               		return "<label> <input class='slaver2' type='checkbox'> <span class='text'></span> </label>";
       		               	}},
       		               	{ "mDataProp": "sysDepartment.fullName","bSortable": true,'sClass': "text-center"},
       		               	{ "mDataProp": "sysDepartment.fullName","bSortable": true,'sClass': "text-center"},
       		               	{ "mDataProp": "rewardType","bSortable": true,'sClass': "text-center"}, 
       		               	{ "mDataProp": "eventDate","bSortable": true,'sClass': "text-center"},
       		               	{ "mDataProp": "submitDate","bSortable": true,'sClass': "text-center"},
       		               	{ "mDataProp": "dealDate","bSortable": true,'sClass': "text-center"},
       		               	{ "mDataProp": "sysUser.realName","bSortable": true,'sClass': "text-center"},
       		               	{ "mDataProp": "rewardMoney","bSortable": true,'sClass': "text-center"},
       		               	{ "mDataProp": "effectDate","bSortable": false,'sClass': "text-center"}
       		   			],
	   			"aoColumnDefs": [{
	   	            sDefaultContent: '',
	   	            aTargets: ['_all']
	   	        }],
            	
            });

        }

    };

}();

InitiatePunishDataTable.init();

function retrieveData2( sSource, aoData, fnCallback, oSettings ) {  
	
    aoData.push( { "name": "pageNum", "value": (Math.ceil( oSettings._iDisplayStart / oSettings._iDisplayLength )+1) });
    aoData.push( { "name": "pageSize", "value": oSettings._iDisplayLength });  
    
    var dateRange = $('#reservation2').val();
    var searchVal = $('#searchVal2').val();
    
    if(dateRange && dateRange.length != 0){
    	var minDate = trim(dateRange.split("-")[0]);
        var maxDate = trim(dateRange.split("-")[1]);
        
        aoData.push({ "name": "minDate", "value": minDate });
        aoData.push({ "name": "maxDate", "value": maxDate });
    }
    
    if(searchVal && searchVal.length != 0){
    	aoData.push({ "name": "searchVal", "value": searchVal });
    }
    
    $.ajax( {  
        "url": sSource,  
        "data": aoData,  
        "cache": false,  
        "dataType": 'json', 
        "type": "POST", 
        "success" :function(response) {  
        	fnCallback(response.returnObject);
        }  
    } );  
}  
//回车搜索
function search(num){
	if(event.keyCode==13){
		if(num==1){
			InitiateRewardsDataTable.init();
		}else if(num==2){
			InitiatePunishDataTable.init();
		}
	}
}